using ERP_SaaS_WinForm.生产计划_派单_车间.ProductPlan;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using CCWin;

namespace ERP_SaaS_WinForm.生产计划_派单_车间
{
    public partial class ProductInfoForm : CCSkinMain
    {
        private List<Product> productList;

        public ProductInfoForm(List<Product> products)
        {
            this.productList = products ?? new List<Product>();
            InitializeComponent();
            InitializeForm();
        }

        private void InitializeForm()
        {
            this.Text = "产品信息详情";
            this.Size = new Size(800, 600);
            this.StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;

            LoadProductData();
        }

        private void LoadProductData()
        {
            try
            {
                if (productList == null || productList.Count == 0)
                {
                    ShowAlert("暂无产品数据", "提示");
                    return;
                }

                // 绑定数据到DataGridView
                dgvProductInfo.AutoGenerateColumns = false;
                dgvProductInfo.DataSource = productList;

                // 设置列标题
                if (dgvProductInfo.Columns.Count == 0)
                {
                    dgvProductInfo.Columns.Add(new DataGridViewTextBoxColumn
                    {
                        Name = "ProductId",
                        HeaderText = "产品ID",
                        DataPropertyName = "ProductId",
                        Width = 80
                    });

                    dgvProductInfo.Columns.Add(new DataGridViewTextBoxColumn
                    {
                        Name = "ProductCode",
                        HeaderText = "产品编号",
                        DataPropertyName = "ProductCode",
                        Width = 120
                    });

                    dgvProductInfo.Columns.Add(new DataGridViewTextBoxColumn
                    {
                        Name = "ProductName",
                        HeaderText = "产品名称",
                        DataPropertyName = "ProductName",
                        Width = 150
                    });

                    dgvProductInfo.Columns.Add(new DataGridViewTextBoxColumn
                    {
                        Name = "Specification",
                        HeaderText = "规格型号",
                        DataPropertyName = "Specification",
                        Width = 120
                    });

                    dgvProductInfo.Columns.Add(new DataGridViewTextBoxColumn
                    {
                        Name = "UnitName",
                        HeaderText = "单位",
                        DataPropertyName = "UnitName",
                        Width = 80
                    });
                }

                lblProductCount.Text = $"共 {productList.Count} 个产品";
                ShowAlert($"成功加载 {productList.Count} 个产品信息", "提示");
            }
            catch (Exception ex)
            {
                ShowAlert($"加载产品数据失败: {ex.Message}", "错误");
            }
        }

        /// <summary>
        /// 显示提示信息
        /// </summary>
        private void ShowAlert(string message, string caption)
        {
            MessageBox.Show(message, caption, MessageBoxButtons.OK, 
                caption == "成功" ? MessageBoxIcon.Information 
                : caption == "提示" ? MessageBoxIcon.Information 
                : MessageBoxIcon.Error);
        }

        /// <summary>
        /// 关闭按钮
        /// </summary>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 导出按钮
        /// </summary>
        private void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                ShowAlert("导出功能开发中...", "提示");
                // TODO: 实现导出功能
            }
            catch (Exception ex)
            {
                ShowAlert($"导出失败: {ex.Message}", "错误");
            }
        }

        /// <summary>
        /// 搜索按钮
        /// </summary>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                string searchText = txtSearch.Text.Trim();
                if (string.IsNullOrEmpty(searchText))
                {
                    dgvProductInfo.DataSource = productList;
                    return;
                }

                var filteredProducts = productList.Where(p => 
                    p.ProductName.Contains(searchText) || 
                    p.ProductCode.Contains(searchText) ||
                    p.Specification.Contains(searchText)
                ).ToList();

                dgvProductInfo.DataSource = filteredProducts;
                lblProductCount.Text = $"共 {filteredProducts.Count} 个产品";
                ShowAlert($"找到 {filteredProducts.Count} 个匹配的产品", "提示");
            }
            catch (Exception ex)
            {
                ShowAlert($"搜索失败: {ex.Message}", "错误");
            }
        }

        /// <summary>
        /// 重置搜索按钮
        /// </summary>
        private void btnReset_Click(object sender, EventArgs e)
        {
            txtSearch.Text = string.Empty;
            dgvProductInfo.DataSource = productList;
            lblProductCount.Text = $"共 {productList.Count} 个产品";
        }

        /// <summary>
        /// 双击产品行显示详细信息
        /// </summary>
        private void dgvProductInfo_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex >= 0)
                {
                    var product = dgvProductInfo.Rows[e.RowIndex].DataBoundItem as Product;
                    if (product != null)
                    {
                        string detailInfo = $"产品详细信息：\n\n" +
                            $"产品ID: {product.ProductId}\n" +
                            $"产品编号: {product.ProductCode}\n" +
                            $"产品名称: {product.ProductName}\n" +
                            $"规格型号: {product.Specification}\n" +
                            $"单位: {product.UnitName}";

                        ShowAlert(detailInfo, "产品详情");
                    }
                }
            }
            catch (Exception ex)
            {
                ShowAlert($"显示产品详情失败: {ex.Message}", "错误");
            }
        }
    }
} 